package com.ibiaoke.customer.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.customer.entity.PtOpenGroup;
import com.ibiaoke.customer.entity.PtOpenGroupExample;
import org.springframework.beans.factory.annotation.Autowired;
import com.ibiaoke.customer.mapper.PtOpenGroupMapper;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Repository
public class PtOpenGroupDao extends BaseDao<PtOpenGroup,PtOpenGroupExample,Integer,PtOpenGroupMapper>{

    @Autowired
    private PtOpenGroupMapper ptOpenGroupMapper;

    @Override
    public PtOpenGroupMapper getMapper() {
        return ptOpenGroupMapper;
    }


    public Integer savePtOpenGroup(PtOpenGroup ptOpenGroup){
        return ptOpenGroupMapper.insert(ptOpenGroup);
    }

    public List<PtOpenGroup> openGroupList(Integer ptGoodsId, Page page) {
        PtOpenGroupExample example = new PtOpenGroupExample();
        example.createCriteria()
                .andPtGoodsIdEqualTo(ptGoodsId)
                .andIsOpenEqualTo(CommonConsts.YES)
                .andPtCountNotEqualTo(CommonConsts.NO)
                .andIsPayEqualTo(CommonConsts.YES)
                .andCreateTimeLessThanOrEqualTo(new Date())
                .andOverTimeGreaterThanOrEqualTo(new Date());

        page.setTotalCount(ptOpenGroupMapper.countByExample(example));

        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("over_time desc");

        return ptOpenGroupMapper.selectByExample(example);
    }

    public List<PtOpenGroup> getOpenGroup(Integer kid){
        PtOpenGroupExample example = new PtOpenGroupExample();
        PtOpenGroupExample.Criteria criteria = example.createCriteria();
        criteria.andKidEqualTo(kid);
        List<PtOpenGroup> ptOpenGroupList= ptOpenGroupMapper.selectByExample(example);
        return ptOpenGroupList;
    }

    public PtOpenGroup getPtOpenGroupById(Integer id) {
        PtOpenGroupExample example = new PtOpenGroupExample();
        example.createCriteria().andKidEqualTo(id).andCreateTimeLessThanOrEqualTo(new Date()).andOverTimeGreaterThanOrEqualTo(new Date());
        return (PtOpenGroup) getByExample(example);
    }

    public PtOpenGroup getPtOpenGroupByKid(Integer kid){
        return selectByPrimaryKey(kid);
    }

    public Integer updatePtOpenGroupCount(Integer kid,Integer num){
        PtOpenGroup ptOpenGroup = new PtOpenGroup();
        ptOpenGroup.setKid(kid);
        ptOpenGroup.setPtCount(num);
        return updateByPrimaryKeySelective(ptOpenGroup);
    }

    public Integer updatePayTypeAndPayMoney(Integer id,Integer payType,Double payMoney){
        PtOpenGroup ptOpenOrder = new PtOpenGroup();
        ptOpenOrder.setKid(id);
        ptOpenOrder.setPayType(payType);
        ptOpenOrder.setPayMoneny(new BigDecimal(payMoney));
        return updateByPrimaryKeySelective(ptOpenOrder);
    }

    public Integer updateOpenGroupStatus(Integer id, Integer status){
        PtOpenGroup ptOpenOrder = new PtOpenGroup();
        ptOpenOrder.setKid(id);
        ptOpenOrder.setStatus(status);
        return updateByPrimaryKeySelective(ptOpenOrder);
    }

    public Integer deleteOpenGroup(Integer kid, Integer isDelete){
        PtOpenGroup ptOpenGroup = new PtOpenGroup();
        ptOpenGroup.setKid(kid);
        ptOpenGroup.setStatus(isDelete);
        return updateByPrimaryKeySelective(ptOpenGroup);
    }


    public List<PtOpenGroup> getPtOpenGroupByOpenCodeAndPtGoodsId(String openCode, Integer ptGoodsId) {
        PtOpenGroupExample example = new PtOpenGroupExample();
        example.createCriteria()
                .andOpenCodeEqualTo(openCode)
                .andPtGoodsIdEqualTo(ptGoodsId)
                .andStatusEqualTo(CommonConsts.YES)
                .andIsPayEqualTo(CommonConsts.YES)
                .andCreateTimeLessThanOrEqualTo(new Date())
                .andOverTimeGreaterThanOrEqualTo(new Date());
        return ptOpenGroupMapper.selectByExample(example);
    }

    public List<PtOpenGroup> getPtOpenGroupListByOpenCode(String openCode){
        PtOpenGroupExample example = new PtOpenGroupExample();
        PtOpenGroupExample.Criteria criteria = example.createCriteria();
        criteria.andOpenCodeEqualTo(openCode);
        return ptOpenGroupMapper.selectByExample(example);
    }

    public Integer updateIsPay(Integer Kid, Integer status){
        PtOpenGroup PtOpenGroup = selectByPrimaryKey(Kid);
        PtOpenGroup.setKid(Kid);
        PtOpenGroup.setIsPay(status);
        return updateByPrimaryKeySelective(PtOpenGroup);
    }

    public List<PtOpenGroup> bossOpenGroupList(Integer ptGoodsId, Page page) {
        PtOpenGroupExample example = new PtOpenGroupExample();
        PtOpenGroupExample.Criteria criteria = example.createCriteria();
        if (ptGoodsId != null) {
            criteria.andPtGoodsIdEqualTo(ptGoodsId);
        }
        criteria.andIsOpenEqualTo(CommonConsts.YES);
        page.setTotalCount(ptOpenGroupMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("over_time desc");

        return selectByExample(example);
    }

    public List<PtOpenGroup> listOpenGroupByOpenCode(String openCode, Page page) {
        PtOpenGroupExample example = new PtOpenGroupExample();
        example.createCriteria().andOpenCodeEqualTo(openCode);

        page.setTotalCount(ptOpenGroupMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("over_time desc");
        return ptOpenGroupMapper.selectByExample(example);
    }
}
